import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.util.HashMap;
import java.util.Map;

public class ServletFactory {

    private static Map<String, String> urls = new HashMap<>();//url映射servlet-name
    private static Map<String, String> names = new HashMap<>();//servlet-name映射servlet-class
    //web.xml文件路径
    private static String xmlpath = "WebContent\\WEB-INF\\web.xml";

    //输入url得到servelt
    public static Servlet getServlet(String url) throws Exception {
        String servletName = urls.get(url);
        String servletClass = names.get(servletName);
        if (servletClass != null)
        {
            Servlet servlet = (Servlet) Class.forName(servletClass).newInstance();
            return servlet;
        }
        else
            return null;
    }

    //读取web.xml
    static {

        try {
            //解析xml
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true); // never forget this!
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.parse(xmlpath);

            XPathFactory factory = XPathFactory.newInstance(); //创建 XPathFactory
            XPath xpath = factory.newXPath();//用这个工厂创建 XPath 对象

            NodeList nodes = (NodeList) xpath.evaluate("web-app/servlet", doc, XPathConstants.NODESET);
            String name = "";
            String value = "";
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                name = (String) xpath.evaluate("servlet-name", node, XPathConstants.STRING);
                value = (String) xpath.evaluate("servlet-class", node, XPathConstants.STRING);
                System.out.println("servlet-name=" + name + ";servlet-class=" + value);
                names.put(name, value);//servlet-name映射servlet-class
            }
            System.out.println("============================");
            NodeList nodes2 = (NodeList) xpath.evaluate("web-app/servlet-mapping", doc, XPathConstants.NODESET);
            for (int i = 0; i < nodes2.getLength(); i++) {
                Node node = nodes2.item(i);
                name = (String) xpath.evaluate("servlet-name", node, XPathConstants.STRING);
                value = (String) xpath.evaluate("url-pattern", node, XPathConstants.STRING);
                System.out.println("servlet-name=" + name + ";url-pattern=" + value);
                urls.put(value, name);//url映射servlet-name
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //测试读取web xml
    public static void main(String[] args) {

        try {
            //解析文档
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true); // never forget this!
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.parse(xmlpath);

            XPathFactory factory = XPathFactory.newInstance(); //创建 XPathFactory
            XPath xpath = factory.newXPath();//用这个工厂创建 XPath 对象

            NodeList nodes = (NodeList) xpath.evaluate("web-app/servlet", doc, XPathConstants.NODESET);
            String name = "";
            String value = "";
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                name = (String) xpath.evaluate("servlet-name", node, XPathConstants.STRING);
                value = (String) xpath.evaluate("servlet-class", node, XPathConstants.STRING);
                System.out.println("servlet-name=" + name + ";servlet-class=" + value);
                names.put(name, value);
            }
            System.out.println("============================");
            NodeList nodes2 = (NodeList) xpath.evaluate("web-app/servlet-mapping", doc, XPathConstants.NODESET);
            for (int i = 0; i < nodes2.getLength(); i++) {
                Node node = nodes2.item(i);
                name = (String) xpath.evaluate("servlet-name", node, XPathConstants.STRING);
                value = (String) xpath.evaluate("url-pattern", node, XPathConstants.STRING);
                System.out.println("servlet-name=" + name + ";url-pattern=" + value);
                urls.put(name, value);
            }


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
